package com.cloud.job.utils;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author 杨斌
 * 2024-12-2024-12-10
 */
public class DateUtils {

    private static final DateTimeFormatter INPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    public static String formatToDt(LocalDateTime dateTime) {
        return dateTime.format(OUTPUT_FORMATTER);
    }

    public static LocalDateTime parseLogoutTime(String logoutTimeStr) {
        return LocalDateTime.parse(logoutTimeStr, INPUT_FORMATTER);
    }
    public static LocalDateTime toLocalDateTime(long timestampInMillis) {
        Instant instant = Instant.ofEpochMilli(timestampInMillis);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /**
     * 将字符串格式的年月日时分秒转换为 LocalDate 对象
     *
     * @param dateTimeStr 输入的日期时间字符串，格式为 "yyyy-MM-dd HH:mm:ss"
     * @return 转换后的 LocalDate 对象
     */
    public static LocalDate parseToLocalDate(String dateTimeStr) {
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, INPUT_FORMATTER);
        return dateTime.toLocalDate();
    }
    /**
     * 将字符串格式的年月日时分秒转换为年月日
     *
     * @param dateTimeStr 输入的日期时间字符串，格式为 "yyyy-MM-dd HH:mm:ss"
     * @return 格式化后的年月日字符串，格式为 "yyyy-MM-dd"
     */
    public static String parseAndFormatToDt(String dateTimeStr) {
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, INPUT_FORMATTER);
        return dateTime.format(OUTPUT_FORMATTER);
    }

}
